package com.example.wangzh.developkit.control;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.base.BaseActivity;

import butterknife.BindView;

/**
 * Created by zhwang16 on 2018/2/12.
 */

public class KeyBoardActivity extends BaseActivity {
    @BindView(R.id.keyboard_view)
    KeyboardView keyboardView;
    @BindView(R.id.edt)
    EditText edt;
    int mPrimaryCode;
    long pressTime;
    private static final long LONG_CLICK_INTERVAL = 500;
    private Keyboard k1;// 字母键盘
    private static final int SYMBOL_CODE = -7;//符号键盘
    private static final int ELLIPSES_CODE = -8;//省略号
    private static final int CHINESE_HORIZONTAL_LINE_CODE = -9;//中文横线
    private static final int SMILING_FACE_CODE = -10;//笑脸
    private static final int LEFT_CODE = -11;//中文横线
    private static final int RIGHT_CODE = -12;//中文横线
    private static final int HEE_CODE = -13;//哈哈
    private static final int AWKWARD_CODE = -14;//尴尬

    @Override
    protected int getLayout() {
        return R.layout.activity_key_board;
    }

    @Override
    protected void initView() {
        k1 = new Keyboard(this, R.xml.keyboard_number2);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(false);
        //点击时不弹出对话框
        keyboardView.setPreviewEnabled(false);
        KeyboardView.OnKeyboardActionListener onKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
            @Override
            public void swipeUp() {
            }

            @Override
            public void swipeRight() {
            }

            @Override
            public void swipeLeft() {
            }

            @Override
            public void swipeDown() {
            }

            @Override
            public void onText(CharSequence text) {
            }

            @Override
            public void onRelease(int primaryCode) {

            }

            @Override
            public void onPress(int primaryCode) {
                mPrimaryCode = primaryCode;
                pressTime = System.currentTimeMillis();
            }

            @Override
            public void onKey(int primaryCode, int[] keyCodes) {
                if (System.currentTimeMillis() - pressTime > LONG_CLICK_INTERVAL) {
                    performLongClick(primaryCode, keyCodes);
                } else {
                    performClick(primaryCode, keyCodes);
                }
            }
        };
        keyboardView.setOnKeyboardActionListener(onKeyboardActionListener);
    }

    private void performClick(int primaryCode, int[] keyCodes) {
        if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
        } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退

        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换

        } else if (primaryCode == SYMBOL_CODE) {// 符号键盘

        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换

        } else if (primaryCode == LEFT_CODE) { //向左
            if (edt.length() == 0) {
                return;
            }
            edt.getEditableText().delete(edt.length() - 1, edt.length());
        } else if (primaryCode == RIGHT_CODE) { // 向右

        } else if (primaryCode == ELLIPSES_CODE) { //省略号
        } else if (primaryCode == CHINESE_HORIZONTAL_LINE_CODE) {
        } else if (primaryCode == SMILING_FACE_CODE) {
        } else if (primaryCode == HEE_CODE) {
        } else if (primaryCode == AWKWARD_CODE) {
        } else {
            String str = Character.toString((char) primaryCode);
            edt.append(str);
//                    if (isWord(str)) {
//                    }
        }
    }

    private void performLongClick(int primaryCode, int[] keyCodes) {
        if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
        } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退

        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换

        } else if (primaryCode == SYMBOL_CODE) {// 符号键盘

        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换

        } else if (primaryCode == LEFT_CODE) { //向左
            edt.setText("");
        } else if (primaryCode == RIGHT_CODE) { // 向右

        } else if (primaryCode == ELLIPSES_CODE) { //省略号
        } else if (primaryCode == CHINESE_HORIZONTAL_LINE_CODE) {
        } else if (primaryCode == SMILING_FACE_CODE) {
        } else if (primaryCode == HEE_CODE) {
        } else if (primaryCode == AWKWARD_CODE) {
        } else {
            String str = Character.toString((char) primaryCode);
            edt.append(str);
//                    if (isWord(str)) {
//                    }
        }
    }

    private boolean isWord(String str) {
        return str.matches("[a-zA-Z]");
    }
}
